package com.filldream.fastboot.common.constant.enums;

import cn.hutool.core.util.DesensitizedUtil;
import com.filldream.fastboot.common.annotation.TmAnn;
import com.filldream.fastboot.common.util.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * 脱敏枚举类
 * @author RickSun
 * @date 2023-11-09
 * hutool方法参考：https://plus.hutool.cn/apidocs/ ctrl+f 搜索 DesensitizedUtil 查看脱敏规则
 */
@Getter
@AllArgsConstructor
public enum TmTypeEnum {

    /** 不脱敏，原样返回 **/
    NONE,

    /** 自定义脱敏规则 **/
    DIY,

    /** 脱敏中国手机号 **/
    PHONE,

    /** 脱敏中国身份证 **/
    CHINA_ID_CARD,

    /** 脱敏邮箱 **/
    EMAIL

    /** 脱敏中国姓名 **/
    ,CHINA_NAME

    /** 脱敏密码 **/
    ,PWD

    /** 脱敏银行卡号 **/
    ,BANK_CARD

    /** 脱敏电话 **/
    ,FIXED_PHONE

    /** 自带脱敏名称 **/
    ,FASTBOOT_HIDE_NAME

    /** 脱敏地址 **/
    ,ADDRESS

    ;

    /**
     * 一键脱敏
     * @param tmAnn 脱敏注解
     * @param origin    原字符串
     * @return  脱敏后内容
     */
    public static String tm(TmAnn tmAnn, String origin){
        String result = origin;
        switch (tmAnn.value()) {
            case NONE:
                break;
            case PHONE:
                result = DesensitizedUtil.mobilePhone(origin);
                break;
            case CHINA_NAME:
                result = DesensitizedUtil.chineseName(origin);
                break;
            case CHINA_ID_CARD:
                result = DesensitizedUtil.idCardNum(origin, tmAnn.pre(),tmAnn.suffix());
                break;
            case PWD:
                result = DesensitizedUtil.password(origin);
                break;
            case EMAIL:
                result = DesensitizedUtil.email(origin);
                break;
            case BANK_CARD:
                result = DesensitizedUtil.bankCard(origin);
                break;
            case FIXED_PHONE:
                result = DesensitizedUtil.fixedPhone(origin);
                break;
            case DIY:
                result = diyTm(tmAnn, origin);
                break;
            case FASTBOOT_HIDE_NAME:
                result = StringUtil.hideName(origin);
                break;
            case ADDRESS:
                result = DesensitizedUtil.address(origin, origin.length() - tmAnn.pre());
                break;
            default:
                break;
        }
        //替换脱敏字符串
        return tmAnn.symbol().equals("*") ? result : result.replaceAll( "\\*",tmAnn.symbol() );
    }

    /**
     * 自定义脱敏
     * @param tmAnn 脱敏注解
     * @param origin    原字符串
     * @return  脱敏后的字符串
     */
    public static String diyTm(TmAnn tmAnn,String origin){
        if(StringUtils.isBlank(origin)){
            return origin;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0, n = origin.length(); i < n; i++) {
            if (i < tmAnn.pre()) {
                sb.append(origin.charAt(i));
                continue;
            }
            if (i > (n - tmAnn.suffix() - 1)) {
                sb.append(origin.charAt(i));
                continue;
            }
            sb.append(tmAnn.symbol());
        }
        return sb.toString();
    }

}
